//
//  SettlementController.m
//  HarmonizePub
//
//  Created by rzhy on 16/8/25.
//  Copyright © 2016年 Zy. All rights reserved.
//

#import "SettlementController.h"
#import <UIScrollView+EmptyDataSet.h>
#import "Config.h"
#import "SettlementViewModel.h"
#import "SettlementTableViewCell.h"

static NSString * const CellIdentifier = @"Cell";

@interface SettlementController () <UITableViewDataSource, UITableViewDelegate, SettlementTableViewCellDelegate, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
{
    BOOL           _paymentState;
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) SettlementViewModel *viewModel;

@end

@implementation SettlementController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
//    self.title = @"门诊结算";
    self.tableView.emptyDataSetSource = self;
    self.tableView.emptyDataSetDelegate = self;
    UINib *nib = [UINib nibWithNibName:[SettlementTableViewCell registerName] bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:CellIdentifier];
    [self createSegmentedControl];
    self.viewModel = [[SettlementViewModel alloc] init];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)loadData {
    [self.viewModel loadDataWithCompleted:^{
        [_tableView reloadData];
    }];
}

#pragma mark - SegmentedControl
- (void)createSegmentedControl {
    UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:@[@"支付项", @"支付历史"]];
    seg.selectedSegmentIndex = 0;
    [seg addTarget:self action:@selector(switchSelectEvent:) forControlEvents:UIControlEventValueChanged];
    self.navigationItem.titleView = seg;
}

- (IBAction)switchSelectEvent:(UISegmentedControl *)sender {
    _paymentState = sender.selectedSegmentIndex;
    [self.tableView reloadData];
}

#pragma mark - tableView Delegate/DataSource Method
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.viewModel numberOfRowsInSection];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [SettlementTableViewCell cellHeight];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SettlementTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell.cellDelegate = self;
    cell.paymentState = _paymentState;
    cell.settlement = [self.viewModel modelInIndexPath:indexPath];
    return cell;
}

- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
    return NO;
}

- (void)tableViewCell:(SettlementTableViewCell *)cell didPaymentEventAtSettlement:(Settlement *)settlement{
    XHLog(@"settlement: %@", settlement);
}

#pragma mark - DZNEmptyDataSetSource Method
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    UIImage *image = [UIImage imageNamed:@"LoadFailure.png"];
    return image;
}

- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *string = XHEmptyDes;
    NSAttributedString *abs = [[NSAttributedString alloc] initWithString:string attributes:nil];
    return abs;
}

- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView {
    return -64.0f;
}

#pragma mark - DZNEmptyDataSetDelegate Method
- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView {
    return YES;
}

- (BOOL)emptyDataSetShouldAllowTouch:(UIScrollView *)scrollView {
    return YES;
}

- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView {
    return YES;
}

- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view {
//     [self getMyConfirmList];
}

@end
